package com.fc.test.service;

import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.fc.test.mapper.auto.*;
import com.fc.test.model.auto.*;
import com.fc.test.util.DateUtil;
import com.fc.test.util.OrderNOUtil;
import com.fc.test.util.RedPackageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.ldap.embedded.EmbeddedLdapProperties;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.fc.test.common.base.BaseService;
import com.fc.test.common.support.Convert;
import com.fc.test.model.custom.Tablepar;
import com.fc.test.util.SnowflakeIdWorker;
import org.springframework.transaction.annotation.Transactional;

/**
 * Service
* @Title: TAbbQuanminService.java 
* @Package com.fc.test.entity 
* @author fuce
* @email 87766867@qq.com
* @date 2019-08-19 15:25:48  
 */
@Service
public class TAbbQuanminService implements BaseService<TAbbQuanmin, TAbbQuanminExample>{
	@Autowired
	private TAbbQuanminMapper tAbbQuanminMapper;

	@Autowired
	private TAbbMemberMapper tAbbMemberMapper;

	@Autowired
	private TAbbOrderMapper orderMapper;
	@Autowired
	private TAbbCommissionMapper commissionMapper;
	@Autowired
	private TAbbCommissionMapper tAbbCommissionMapper;

	@Autowired
	private TAbbTgzifeiMapper tgzifeiMapper;
	/**
	 * 分页查询
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	 public PageInfo<TAbbQuanmin> list(Tablepar tablepar,String name){
	        TAbbQuanminExample testExample=new TAbbQuanminExample();
	        testExample.setOrderByClause("id ASC");
		 TAbbQuanminExample.Criteria criteria=testExample.createCriteria();
	        if(name!=null&&!"".equals(name)){
				criteria.andTitleLike("%"+name+"%");
	        }
		    criteria.andYuliu2EqualTo("1");
	        PageHelper.startPage(tablepar.getPageNum(), tablepar.getPageSize());
	        List<TAbbQuanmin> list= tAbbQuanminMapper.selectByExample(testExample);
	        PageInfo<TAbbQuanmin> pageInfo = new PageInfo<TAbbQuanmin>(list);
	        return  pageInfo;
	 }

	@Override
	public int deleteByPrimaryKey(String ids) {
		List<String> lista=Convert.toListStrArray(ids);
		for (String id:lista){
			TAbbOrderExample example=new TAbbOrderExample();
			TAbbOrderExample.Criteria criteria=example.createCriteria();
			criteria.andObjidEqualTo(id);
			criteria.andTypeEqualTo(2);
			List<TAbbOrder> orderList=orderMapper.selectByExample(example);
			if (orderList!=null && orderList.size()>0){
				for (TAbbOrder tAbbOrder:orderList){
					if (tAbbOrder.getRenwumoney()>0){
						//返回任务余额到用户账号
						TAbbMember tAbbMember=tAbbMemberMapper.selectByPrimaryKey(tAbbOrder.getUserid());
						TAbbMember tAbbMember3=new TAbbMember();
						tAbbMember3.setId(tAbbMember.getId());
						tAbbMember3.setAccount(tAbbMember.getAccount()+tAbbOrder.getRenwumoney());
						tAbbMemberMapper.updateByPrimaryKey(tAbbMember3);

						//返回任务余额到用户账号的记录
						TAbbCommission tAbbCommission3=new TAbbCommission();
						tAbbCommission3.setCreattime(DateUtil.getCurrentDateTimeStr());
						tAbbCommission3.setType(1);
						tAbbCommission3.setBeiyongone(String.valueOf(tAbbOrder.getRenwumoney()));
						tAbbCommission3.setBusinessid("3");
						tAbbCommission3.setMemberid(tAbbMember.getId());
						tAbbCommission3.setMessage("消除任务");
						tAbbCommission3.setId(SnowflakeIdWorker.getUUID());
						tAbbCommissionMapper.insertSelective(tAbbCommission3);
					}
				}
			}
		}
		TAbbQuanminExample example=new TAbbQuanminExample();
		example.createCriteria().andIdIn(lista);
		return tAbbQuanminMapper.deleteByExample(example);
	}


	@Override
	public TAbbQuanmin selectByPrimaryKey(String id) {

		return tAbbQuanminMapper.selectByPrimaryKey(id);
	}


	@Override
	public int updateByPrimaryKeySelective(TAbbQuanmin record) {
		return tAbbQuanminMapper.updateByPrimaryKeySelective(record);
	}

	/**
	 * 添加
	 */
	@Override
	public int insertSelective(TAbbQuanmin record) {
		//添加雪花主键id
		record.setId(SnowflakeIdWorker.getUUID());
		return tAbbQuanminMapper.insertSelective(record);
	}










	@Override
	public int updateByExampleSelective(TAbbQuanmin record, TAbbQuanminExample example) {

		return tAbbQuanminMapper.updateByExampleSelective(record, example);
	}


	@Override
	public int updateByExample(TAbbQuanmin record, TAbbQuanminExample example) {

		return tAbbQuanminMapper.updateByExample(record, example);
	}

	@Override
	public List<TAbbQuanmin> selectByExample(TAbbQuanminExample example) {

		return tAbbQuanminMapper.selectByExample(example);
	}


	@Override
	public long countByExample(TAbbQuanminExample example) {

		return tAbbQuanminMapper.countByExample(example);
	}


	@Override
	public int deleteByExample(TAbbQuanminExample example) {

		return tAbbQuanminMapper.deleteByExample(example);
	}


	public List<TAbbQuanmin> getQuanminByPage(int page,String memberId){
		Map<String,Object> paramMap = new HashMap<>();
		paramMap.put("page",page);
		List<TAbbQuanmin> list = tAbbQuanminMapper.getQuanminByPage(paramMap);
		for(TAbbQuanmin quanmin:list){
			//查询用户今天是否已经领过红包
			Map<String,Object> map = new HashMap<>();
			map.put("memberId",memberId);
			map.put("bmid",quanmin.getId());
			int count = commissionMapper.getTodayBmTimes(map);
			if(count>0){
				quanmin.setIsReceive(1);
			}else{
				quanmin.setIsReceive(0);
			}
		}
		return list;
	}

	@Transactional
	public Map<String,Object>  releaseQuanmin(TAbbQuanmin quanmin){

		Map<String,Object> resMap = new HashMap<>();
		TAbbMember member = tAbbMemberMapper.selectByPrimaryKey(quanmin.getUserid());
		TAbbTgzifei tAbbTgzifei = tgzifeiMapper.selectTgzifei();

		//判断输入的佣金是否大于最低金额
		double leftScore = 100-Double.parseDouble(tAbbTgzifei.getShangjiscore())-Double.parseDouble(tAbbTgzifei.getPingtaiscore());
		double minMoney = (0.05*quanmin.getZhuantimes())/(leftScore/100);
		DecimalFormat df = new DecimalFormat("#.00");
		if(quanmin.getPrice()<minMoney){
			resMap.put("success",false);
			resMap.put("msg","最低金额需大于"+df.format(minMoney)+"元！");
			return resMap;
		}

		quanmin.setId(SnowflakeIdWorker.getUUID());
		quanmin.setBlance(quanmin.getPrice());
		quanmin.setCreattime(DateUtil.getCurrentDateTimeStr());
		quanmin.setRemaintimes(quanmin.getZhuantimes());
		quanmin.setYuliu1(member.getNicename());
		quanmin.setYuliu2("0");
		int flag = tAbbQuanminMapper.insert(quanmin);


		//创建order订单------------------------------------------------------------------
		TAbbOrder order = new TAbbOrder();
		order.setId(SnowflakeIdWorker.getUUID());
		order.setCreatetime(DateUtil.getCurrentDateTimeStr());
		order.setUserid(quanmin.getUserid());
		order.setObjid(quanmin.getId());
		order.setPaymoney(quanmin.getPrice());
		order.setRenwumoney(quanmin.getPrice());
		order.setOrderno(OrderNOUtil.getOrderNoByTime());
		order.setType(4);
		order.setPaystatus(0);
		order.setYuliu3(quanmin.getTitle());
		order.setYuliu4(member.getNicename());
		orderMapper.insert(order);

		if(flag>0){
			resMap.put("success",true);
			resMap.put("msg","发布成功");
			resMap.put("orderid",order.getId());
		}else{
			resMap.put("success",false);
			resMap.put("msg","发布失败");
		}
		return resMap;
	}

	public Map<String,Object> getQuanminDetail(String qmid,String userid){
		Map<String,Object> resMap = new HashMap<>();
		TAbbQuanmin quanmin = tAbbQuanminMapper.selectByPrimaryKey(qmid);
		Map<String,Object> paramMap = new HashMap<>();
		paramMap.put("memberId",userid);
		paramMap.put("bmid",qmid);
		int count = commissionMapper.getTodayBmTimes(paramMap);
		if(count==0&&quanmin.getBlance()>0){
			quanmin.setIsReceive(0);
		}else{
			quanmin.setIsReceive(1);
		}
		/*resMap.put("redPcket","");

		//查询用户今天是否已经领过红包
		Map<String,Object> paramMap = new HashMap<>();
		paramMap.put("memberId",userid);
		paramMap.put("bmid",qmid);
		int count = commissionMapper.getTodayBmTimes(paramMap);
		if(count==0){//今天没有领过红包
			//本条便民信息剩余的红包金额
			double balance = quanmin.getBlance();
			//本条便民信息剩余的红包个数
			int leftNum = quanmin.getRemaintimes();

			//获得本次红包金额
			double redPcket = RedPackageUtil.doIt(balance,leftNum);
			DecimalFormat df = new DecimalFormat("#.00");
			redPcket = Double.parseDouble(df.format(redPcket));
			balance = Double.parseDouble(df.format(balance-redPcket));
			leftNum -= 1;
			quanmin.setBlance(balance);
			quanmin.setRemaintimes(leftNum);
			tAbbQuanminMapper.updateByPrimaryKey(quanmin);

			//给用户钱包加上红包金额--------------------------------------------------
			TAbbMember member = tAbbMemberMapper.selectByPrimaryKey(userid);
			member.setAccount(member.getAccount()+redPcket);
			tAbbMemberMapper.updateByPrimaryKey(member);

			//加流水-------------------------------------------------------------------
			TAbbCommission commission = new TAbbCommission();
			commission.setId(SnowflakeIdWorker.getUUID());
			commission.setType(1);
			commission.setBusinessid("1");
			commission.setMemberid(userid);
			commission.setBeiyongtwo(qmid);
			commission.setBeiyongone(redPcket+"");
			commission.setCreattime(DateUtil.getCurrentDateTimeStr());
			commission.setMessage("任务奖励");
			tAbbCommissionMapper.insert(commission);

			resMap.put("redPcket",redPcket);
		}*/

		resMap.put("success",true);
		resMap.put("msg","获取全民推广详情成功");
		resMap.put("data",quanmin);
		return resMap;
	}

	public Map<String,Object> getQuanminRedPcket(String qmid,String userid){
		Map<String,Object> resMap = new HashMap<>();
		TAbbQuanmin quanmin = tAbbQuanminMapper.selectByPrimaryKey(qmid);
		resMap.put("redPcket","");

		//查询用户今天是否已经领过红包
		Map<String,Object> paramMap = new HashMap<>();
		paramMap.put("memberId",userid);
		paramMap.put("bmid",qmid);
		int count = commissionMapper.getTodayBmTimes(paramMap);
		if(count==0){//今天没有领过红包
			//本条便民信息剩余的红包金额
			double balance = quanmin.getBlance();
			//本条便民信息剩余的红包个数
			int leftNum = quanmin.getRemaintimes();

			//获得本次红包金额
			double redPcket = RedPackageUtil.doIt(balance,leftNum);
			DecimalFormat df = new DecimalFormat("#.00");
			redPcket = Double.parseDouble(df.format(redPcket));
			balance = Double.parseDouble(df.format(balance-redPcket));
			leftNum -= 1;
			quanmin.setBlance(balance);
			quanmin.setRemaintimes(leftNum);
			tAbbQuanminMapper.updateByPrimaryKey(quanmin);

			//给用户钱包加上红包金额--------------------------------------------------
			TAbbMember member = tAbbMemberMapper.selectByPrimaryKey(userid);
			member.setAccount(member.getAccount()+redPcket);
			tAbbMemberMapper.updateByPrimaryKey(member);

			//加流水-------------------------------------------------------------------
			TAbbCommission commission = new TAbbCommission();
			commission.setId(SnowflakeIdWorker.getUUID());
			commission.setType(1);
			commission.setBusinessid("1");
			commission.setMemberid(userid);
			commission.setBeiyongtwo(qmid);
			commission.setBeiyongone(redPcket+"");
			commission.setCreattime(DateUtil.getCurrentDateTimeStr());
			commission.setMessage("任务奖励");
			tAbbCommissionMapper.insert(commission);

			resMap.put("redPcket",redPcket);
		}
		resMap.put("success",true);
		resMap.put("msg","获取全民推广红包成功");
		return resMap;
	}

	public List<TAbbQuanmin> getMyQuanminList(int page,String userid){
		Map<String,Object> paramMap = new HashMap<>();
		paramMap.put("page",page);
		paramMap.put("userid",userid);
		List<TAbbQuanmin> list = tAbbQuanminMapper.getMyQuanminList(paramMap);
		return list;
	}


}
